fix(dex): cap DEX order settlement per block to prevent consensus livelock - #475
Merged
Conversation
ezeike
approved these changes
Jul 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fix(dex): cap DEX order settlement per block to prevent consensus livelock
A released order backlog was locked into a single 830-order batch. Settling
it in begin_block took ~5.5s per ApplyBlock (paid twice on a single-validator
nested chain: once building, once validating), pushing the full BFT cycle past
the ~20s round budget. Because every root block resets the nested chain to
round 0 (NEW_COMMITTEE -> NewHeight(true)), the (2*round+1) timer escalation
could never kick in, so the chain livelocked at height 736327 and never
committed.
Bound the work per block with MaxOrdersSettledPerBlock (250). After the
pseudorandom sort, HandleDexBatchOrders settles only the first N orders and
breaks; overflow orders keep a zero receipt and are refunded to the seller on
the origin chain via HandleOrderReceipts (same path as a slippage failure).
The overflow path does no work on the nested chain (no AMM math, pool writes,
or events), so begin_block stays within the round budget while the oversized
batch still fully resolves - and rotates - in a single block.
Tests: add TestHandleDexBatchOrdersEnforcesSettlementCap (exactly the cap
settles; the remainder gets zero receipts for refund).